package cn.sinata.wanglin.publish.activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.google.gson.JsonObject;
import com.xilada.xldutils.activitys.TitleActivity;
import com.xilada.xldutils.utils.BitmapUtils;
import com.xilada.xldutils.utils.SharedPreferencesUtils;
import com.xilada.xldutils.utils.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.sinata.wanglin.MyApplication;
import cn.sinata.wanglin.R;
import cn.sinata.wanglin.aliyunoss.OssBean;
import cn.sinata.wanglin.aliyunoss.UpLoadImageUtils;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.model.Pubactive;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;

/**
 * 活动描述
 */
public class AcitivityremarekActivity extends TitleActivity {
    @BindView(R.id.ed_remark)
    EditText ed_remark;//

    Pubactive mPubactive;
    @Override
    protected int setContentId() {
        return R.layout.activity_acitivityremarek;
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("活动描述");
        addRightButton("提交", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                view.setEnabled(false);
                if(TextUtils.isEmpty(ed_remark.getText().toString())){
                    showToast("活动描述不能为空！");
                    view.setEnabled(true);
                    return;
                }
                showDialog("发布中...");
                update(view,mPubactive.getPath());
            }
        });
        Bundle mBundle=getIntent().getExtras();
        if(mBundle!=null){
            mPubactive= (Pubactive) mBundle.getSerializable("mPubactive");
        }

    }

    /**
     * 发布活动
     */
    private void pubActivity(String path,String scale) {
        String userid= SharedPreferencesUtils.getString(Constant.SP_USERID);
        HttpManager.addActivity(userid,MyApplication.houseid,mPubactive.getContent(),mPubactive.getStartTime(),
                mPubactive.getEndTime(),mPubactive.getAddress(),mPubactive.getMoney(),
                mPubactive.getPeoplenumber(),mPubactive.getPhone(),ed_remark.getText().toString(),path,scale,mPubactive.getLongitude(),mPubactive.getLatitude()).subscribe(new ResultDataSubscriber<JsonObject>(AcitivityremarekActivity.this) {
            @Override
            public void onSuccess(String msg, JsonObject jsonObject) {
                showToast(msg);
                setResult(RESULT_OK);
                finish();
            }
        });
    }
    private void update(final View view,final String path){
        Utils.systemErr("-path-->"+path);
        final BitmapUtils.Image mImage= BitmapUtils.compressImage(path);
        final ArrayList<OssBean> mOssBeans=new ArrayList<>();
        mOssBeans.add(new OssBean(mImage.getPath()));
        Observable<List<OssBean>> mObservable= new UpLoadImageUtils(mOssBeans).uploadimg();
        mObservable.doOnSubscribe(new Action0() {
            @Override
            public void call() {
            }
        })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<List<OssBean>>() {
                    @Override
                    public void onCompleted() {
                        view.setEnabled(true);

                    }
                    @Override
                    public void onError(Throwable e) {
                        dismissDialog();
                    }

                    @Override
                    public void onNext(List<OssBean> ossBeen) {
                        if(ossBeen!=null&&ossBeen.size()!=0){
                            pubActivity(ossBeen.get(0).getUrlkey(),""+mImage.getRate());
                        }
                    }
                });


    }
}
